﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CMS_GYM
{
    public partial class ucBaoCaoBanHangHangNgay : UserControl
    {

        #region Khai bao bien toan cuc
        DateTime? TuNgay;
        DateTime? DenNgay;
        string TuSo;
        string DenSo;
        string KyHieu;
        int? NguoiTaoId;
        string MaKhachHang;
        string TuKhoa ;
        string LoaiPhieu ;
        string TaiKhoanNo;
        string TaiKhoanCo;


        private DataTable _dtBaoCao;
        private int _filterIndex;

        #endregion

        public ucBaoCaoBanHangHangNgay()
        {
            InitializeComponent();

            //this.Load += new EventHandler(ucBaoCaoBanHangDichVuTHC_PhieuThu_Load);
            //this.chk_XemTatCa.CheckedChanged += new EventHandler(chk_XemTatCa_CheckedChanged);
            //this.cbAgent1.SelectedValueChanged += new EventHandler(cbAgent1_SelectedValueChanged);
            //this.btn_ThucHien.Click += new EventHandler(btn_ThucHien_Click);
            //this.radio_BanHangDichVu_Mau1.CheckedChanged += new EventHandler(radio_BanHangDichVu_Mau1_CheckedChanged);
            //this.radio_BanHangDichVu_Mau2.CheckedChanged += new EventHandler(radio_BanHangDichVu_Mau2_CheckedChanged);
            //this.radio_DoanhThu.CheckedChanged += new EventHandler(radio_DoanhThu_CheckedChanged);
            //this.btn_Next.Click += new EventHandler(btn_Next_Click);
            //this.btn_Previous.Click += new EventHandler(btn_Previous_Click);
        }

        /// <summary>
        /// Su kien load page 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ucBaoCaoBanHangDichVuTHC_PhieuThu_Load(object sender, EventArgs e)
        {

        }

        #region MyMethod

        private void GetParams()
        {
            DateTime _TuNgay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            TuNgay = (DateTime?)_TuNgay;
            DateTime _DenNgay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
            DenNgay = (DateTime?)_DenNgay;
            TuSo = null;
            DenSo = null;
            KyHieu = null;
            NguoiTaoId = clsUser.NhanVienId;
            MaKhachHang = null;
            TaiKhoanCo = null;
            TaiKhoanNo = null;
            TuKhoa = null;
            LoaiPhieu = "PhieuThu";

        }

        /// <summary>
        /// Fillter data theo Tai khoan Index
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        private DataTable FilterData(int index)
        {
            try
            {
                if (this._dtBaoCao != null && this._dtBaoCao.Rows.Count > 0)
                {
                    DataTable dtTemp = this._dtBaoCao.Copy();
                    DataTable dtFilter = dtTemp.DefaultView.ToTable(true, "TAIKHOAN_NO", "TAIKHOAN_CO");

                    string TK_NO = "", TK_CO = "";
                    TK_NO = dtFilter.Rows[index - 1]["TAIKHOAN_NO"].ToString();
                    TK_CO = dtFilter.Rows[index - 1]["TAIKHOAN_CO"].ToString();

                    DataView dv = dtTemp.DefaultView;
                    dv.RowFilter = "TAIKHOAN_NO=" + TK_NO + " AND TAIKHOAN_CO=" + TK_CO;

                    //this._taiKhoan = string.Format("TK: {0}/{1}", TK_NO, TK_CO);

                    return dv.ToTable();
                }
            }
            catch (System.Exception e)
            {

            }

            return null;
        }

        /// <summary>
        /// return 0:Mau1,1:Mau2,2:Mau3
        /// </summary>
        /// <returns></returns>
        private int GetReportType()
        {
            //if (radio_BanHangDichVu_Mau1.Checked) return 1;
            //else if (radio_BanHangDichVu_Mau2.Checked) return 2;
            //else if (radio_DoanhThu.Checked) return 3;
            //else return 4;//(radio_DoanhThuTaiKhoan.Checked) 
            return 1;
        }

        /// <summary>
        /// Binding data to report
        /// </summary>
        /// <param name="mode">
        /// 0: BC ban hang DichVu mau 1
        /// 1: BC doanh thu DichVu mau 2
        /// 3: BC doanh thu
        /// </param>
        /// <param name="dt"></param>
        /// <param name="CHI_NHANH"></param>
        /// <param name="tuNgay"></param>
        /// <param name="denNgay"></param>
        /// <param name="TaiKhoan"></param>
        private void FilterDataCrystalReport(int mode, DataTable dt, DateTime? tuNgay, DateTime? denNgay)
        {
            if (radio_ChiTiet.Checked)
            {
                    CMS_GYM.CrystalReports.crpBaoCaoBanHangChiTiet myReport = new CMS_GYM.CrystalReports.crpBaoCaoBanHangChiTiet();
                    myReport.SetDatabaseLogon(clsConnection.LoginName, clsConnection.LoginPassword, clsConnection.ServerName, clsConnection.Databasename);
                    myReport.SetDataSource(dt);
                    myReport.SetParameterValue("TuNgayHead", DateTime.Now.ToString("dd/MM/yyyy"));
                    myReport.SetParameterValue("DenNgayHead", DateTime.Now.ToString("dd/MM/yyyy"));
                    crystalReportViewer1.ReportSource = myReport;
            }
            else
            {
                CMS_GYM.CrystalReports.crpBaoCaoBanHang myReport = new CMS_GYM.CrystalReports.crpBaoCaoBanHang();
                myReport.SetDatabaseLogon(clsConnection.LoginName, clsConnection.LoginPassword, clsConnection.ServerName, clsConnection.Databasename);
                myReport.SetDataSource(dt);
                myReport.SetParameterValue("TuNgayHead", DateTime.Now.ToString("dd/MM/yyyy"));
                myReport.SetParameterValue("DenNgayHead", DateTime.Now.ToString("dd/MM/yyyy"));
                crystalReportViewer1.ReportSource = myReport;
               
            }
        }


        #endregion end of my method

        #region Events


        private void btn_ThucHien_Click(object sender, EventArgs e)
        {
            this._filterIndex = 0;

                try
                {
                    #region Khai bao bien
                    DataTable dtTemp = null;
                    #endregion

                    //Reset params value and set value theo tuy chon giao dien 
                    this.GetParams();

                    this._dtBaoCao = new DataTable();

                    #region Bao cao doanh thu 

                    if (radio_TongHop.Checked)
                    {
                        dtTemp = new clsPhieuThuTienBUS().TimKiemPhieuGiaoDich(TuNgay, DenNgay, TuSo, DenSo, KyHieu, NguoiTaoId, MaKhachHang, TuKhoa,LoaiPhieu);
                        if (dtTemp != null && dtTemp.Rows.Count > 0) _dtBaoCao.Merge(dtTemp);
                    }

                    if (radio_ChiTiet.Checked)
                    {
                        dtTemp = new clsPhieuThuTienBUS().TimKiemPhieuGiaoDichChiTiet(TuNgay, DenNgay, TuSo, DenSo, KyHieu, NguoiTaoId, MaKhachHang,TaiKhoanNo,TaiKhoanCo, TuKhoa, LoaiPhieu);
                        if (dtTemp != null && dtTemp.Rows.Count > 0) _dtBaoCao.Merge(dtTemp);
                    }
                    
                    #endregion 
                    //Bind report datasource
                    FilterDataCrystalReport(1, _dtBaoCao, TuNgay, DenNgay);

                    if (this._dtBaoCao == null || this._dtBaoCao.Rows.Count < 1)
                    {
                        MessageBox.Show("Không có dữ liệu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                catch (Exception ex) { }

        }


        #endregion end of events

    }
}
